package com.fone.player.online.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fone.player.R;
import com.fone.player.activity.BaseActivity;
import com.fone.player.client.Callback;
import com.fone.player.client.Error;
import com.fone.player.client.Request;
import com.fone.player.client.SeeRst;
import com.fone.player.entity.EveryoneItemInfo;
import com.fone.player.online.adapter.EveryOneListAdapter;
import com.fone.player.play.FonePlayerActivity;
import com.fone.player.play.PlayerAction;
import com.fone.player.play.PlayerFrom;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;
import com.fone.player.widget.xlistview.XListView;
import com.fone.player.widget.xlistview.XListView.IXListViewListener;

/**
 * 大家都在看
 * 
 * @author JasonZue
 * @since 2014-5-8
 */

public class EveryoneWatchActivity extends BaseActivity implements
		OnClickListener, OnScrollListener, IXListViewListener {

	private static final String TAG = "EveryoneWatchActivity";

	private Context mContext;

	private TextView mTitleTv, mWatchCountTv;
	private Button mLeftBt, mRightBt;
	private XListView mListView;
	private RelativeLayout mWaitingRl;

	private EveryOneListAdapter mEveryOneListAdapter;
	private EveryoneCallback callback;
	private SeeRst seeRst;
	private List<EveryoneItemInfo> everyoneData = new ArrayList<EveryoneItemInfo>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = getApplicationContext();
		setContentView(R.layout.online_everyone_view);

		initView();
		initData();
	}

	private void initData() {
		callback = new EveryoneCallback();
		// 判断网络状态
		if (FoneUtil.isNetOkWithToast(mContext)) {
			Request.getInstance().seeTmp(1, 1, callback);
			mWaitingRl.setVisibility(View.VISIBLE);
		}
	}

	private void initView() {
		// findID
		mLeftBt = (Button) findViewById(R.id.common_title_left_bt);
		mRightBt = (Button) findViewById(R.id.common_title_right_bt);
		mTitleTv = (TextView) findViewById(R.id.common_title_text_tv);
		mWatchCountTv = (TextView) findViewById(R.id.everyone_count_text_tv);
		mListView = (XListView) findViewById(R.id.everyone_lv);
		mWaitingRl = (RelativeLayout) findViewById(R.id.everyone_waiting);

		// adapter初始化
		mEveryOneListAdapter = new EveryOneListAdapter(mContext, everyoneData);

		// 设置相关属性
		mListView.setAdapter(mEveryOneListAdapter);
		mLeftBt.setText("大家都在看");
		mRightBt.setVisibility(View.GONE);
		mTitleTv.setVisibility(View.GONE);

		// 监听事件
		mLeftBt.setOnClickListener(this);
		mListView.setPullLoadEnable(false);
		mListView.setPullRefreshEnable(true);
		mListView.setEnableDragLoadMore(false);
		mListView.setXListViewListener(this);
		mListView.setOnScrollListener(this);

		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				// 判断网络状态
				if (FoneUtil.isNetOkWithToast(EveryoneWatchActivity.this)) {
					Intent intent = new Intent(mContext,
							FonePlayerActivity.class);

					intent.putExtra("from", PlayerFrom.EVERYONE_WATCH);
					intent.putExtra("action", PlayerAction.FULLPLAY.name());
					intent.putExtra("xyzplay", everyoneData.get(position - 1)
							.getItemUrl());

					startActivity(intent);
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.common_title_left_bt:
			finish();
			break;

		default:
			break;
		}

	}

	@Override
	public void onRefresh() {

		mListView.onLoad();
		// 判断网络状态
		if (FoneUtil.isNetOkWithToast(mContext)) {
			everyoneData.removeAll(everyoneData);

			// 重新请求数据
			Request.getInstance().seeTmp(1, 1, callback);
			mWaitingRl.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onLoadMore() {
		if (null != seeRst && null != seeRst.col && null != seeRst.col.rp
				&& FoneUtil.isNetOkWithToast(mContext)) {
			Request.getInstance().seeTmp(seeRst.col.rp.p, 1, callback);
			mWaitingRl.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {

	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {

	}

	private void setSeeRst(SeeRst seeRst) {

		this.seeRst = seeRst;
	}

	private class EveryoneCallback implements Callback<SeeRst> {

		@Override
		public void onSuccess(SeeRst t) {

			if (t.result == 0) {
				mWaitingRl.setVisibility(View.GONE);
				// 刷新数据时同时修改当前在线人数
				String randomNum = String
						.valueOf((int) (Math.random() * 9999 + 1));
				mWatchCountTv
						.setText("当前有" + randomNum + "位网友正在用100tv观看各类网络视频");

				L.v("测试", "大家都在看", "返回bean:" + t.toString());
				if (null != t) {
					setSeeRst(t);
					seeRst.toString();
					L.v("测试", "大家都在看 seeRst" + seeRst.toString());
				}
				if (null != t.col) {
					if (null != t.col.cnts && null != t.col.cnts.cntList) {
						for (int i = 0; i < t.col.cnts.cntList.size(); i++) {
							EveryoneItemInfo info = new EveryoneItemInfo();
							// 替换地址
							info.setImageUrl(t.shost
									+ FoneUtil.formatUrl(t.col.cnts.cntList
											.get(i).pic1));
							info.setItemContent(t.col.cnts.cntList.get(i).comefrom);
							info.setUserName(t.col.cnts.cntList.get(i).nickname);
							info.setItemName(t.col.cnts.cntList.get(i).name);
							info.setItemUrl(t.col.cnts.cntList.get(i).url);
							everyoneData.add(info);
						}
					}
					// 根据rp中的信息判断是否显示上拉加载更多
					if (null != t.col.rp) {
						if (t.col.rp.p < t.col.rp.m) {
							// 允许
							mListView.setEnableDragLoadMore(true);
							mListView.setPullLoadEnable(true);
						} else {
							// 不允许

							mListView.setEnableDragLoadMore(false);
							mListView.setPullLoadEnable(false);
						}
					}
				} else {
					// 数据返回不正确 不允许上拉加载更多,允许下拉刷新
					mListView.setPullRefreshEnable(false);
					mListView.setEnableDragLoadMore(false);
					mListView.setPullRefreshEnable(true);
				}

				mEveryOneListAdapter.notifyDataSetChanged();
			} else {
				Toast.makeText(mContext, "当前无新内容，请稍后刷新重试", Toast.LENGTH_SHORT)
						.show();

				mWaitingRl.setVisibility(View.GONE);
				mListView.setEnableDragLoadMore(false);
				mListView.setPullLoadEnable(false);
			}
		}

		@Override
		public void onFailure(Error error) {
			L.v(TAG,
					"EveryoneCallback onFailure",
					"isNetworkError : " + error.isNetworkError() + " status : "
							+ error.getStatus() + " reason : "
							+ error.getReason());
			Toast.makeText(mContext, "当前无新内容，请稍后刷新重试", Toast.LENGTH_SHORT)
					.show();

			mWaitingRl.setVisibility(View.GONE);
			mListView.setEnableDragLoadMore(false);
			mListView.setPullLoadEnable(false);
		}

	}

}
